import Mock from 'mockjs'
import fs from 'fs'
import path from 'path'
const jsonFolderPath = path.resolve(__dirname, 'map')

const readJsonFiles = folderPath => {
  const files = fs.readdirSync(folderPath)
  return files.reduce((acc, file) => {
    const filePath = path.join(folderPath, file)
    const fileStat = fs.statSync(filePath)
    if (fileStat.isDirectory()) {
      // 如果是一个文件夹，则递归读取该文件夹中的 JSON 文件
      Object.assign(acc, readJsonFiles(filePath))
    } else if (file.endsWith('.json')) {
      // 如果是一个 JSON 文件，则读取其内容并添加到结果中
      const data = fs.readFileSync(filePath, 'utf-8')
      const keyArr = path.relative(jsonFolderPath, filePath).replace(/\\/g, '/').split('/')
      const key = keyArr[keyArr.length - 1].split('.')[0]
      acc[key] = JSON.parse(data)
    }
    return acc
  }, {})
}

const mapData = readJsonFiles(jsonFolderPath)
export const getMapData = params => {
  console.log(params.query.code)
  const data = mapData[params.query.code]
  const res: any = []
  data['features'].forEach(item => {
    res.push({
      name: item['properties']['name'],
      adcode: item['properties']['adcode'],
      center: item['properties']['center'],
      parent: item['properties']['parent'],
      level: item['properties']['level'],
      value: Mock.mock('@natural(1, 1000)')
    })
  })
  return res
}
